﻿namespace HRM.CORE
{
    using DevExpress.XtraEditors;
    using DevExpress.XtraEditors.Controls;
    using APP.HELPER;
    using HRM.CORE;
    
    using System; using Extra.ERP;
    using System.ComponentModel;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms; using Common;
    using Dictionary.ERP;

    public class xucOrganizationEdit : Common.xucBase
    {
        private IContainer components = null;
        private string m_BranchCode;
        private string m_Code;
        private string m_DepartmentCode;
        private string m_EmployeeCode;
        private string m_GroupCode;
        private int m_Level;
        public HRM.CORE.Organization Organization;
        private PopupContainerControl ppContainer;
        private PopupContainerEdit ppContainerEdit;
        private HRM.CORE.xucOrganization xucOrganization;

        public event DepartmentSelectedHandler DepartmentSelected;

        public xucOrganizationEdit()
        {
            this.InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.ppContainer = new PopupContainerControl();
            this.ppContainerEdit = new PopupContainerEdit();
            this.ppContainer.BeginInit();
            this.ppContainerEdit.Properties.BeginInit();
            base.SuspendLayout();
            this.ppContainer.Location = new Point(0, 0x12);
            this.ppContainer.Name = "ppContainer";
            this.ppContainer.Size = new Size(0x12a, 0x11f);
            this.ppContainer.TabIndex = 1;
            this.ppContainerEdit.Dock = DockStyle.Fill;
            this.ppContainerEdit.Location = new Point(0, 0);
            this.ppContainerEdit.Name = "ppContainerEdit";
            this.ppContainerEdit.Properties.Appearance.Font = new Font("Tahoma", 8.25f, FontStyle.Bold);
            this.ppContainerEdit.Properties.Appearance.Options.UseFont = true;
            this.ppContainerEdit.Properties.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Combo) });
            this.ppContainerEdit.Properties.PopupControl = this.ppContainer;
            this.ppContainerEdit.Size = new Size(0x12a, 20);
            this.ppContainerEdit.TabIndex = 2;
            this.ppContainerEdit.Popup += new EventHandler(this.ppContainerEdit_Popup);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.ppContainerEdit);
            base.Controls.Add(this.ppContainer);
            base.Name = "xucOrganizationEdit";
            base.Size = new Size(0x12a, 20);
            base.Load += new EventHandler(this.xucOrganizationEdit_Load);
            this.ppContainer.EndInit();
            this.ppContainerEdit.Properties.EndInit();
            base.ResumeLayout(false);
        }

        public void LoadData()
        {
            this.Level = MyLogin.Level;
            this.Code = MyLogin.Code;
            if (this.xucOrganization == null)
            {
                this.xucOrganization = new HRM.CORE.xucOrganization();
                this.xucOrganization.Dock = DockStyle.Fill;
                this.ppContainer.Controls.Add(this.xucOrganization);
                if (this.ppContainer.Width < this.ppContainerEdit.Width)
                {
                    this.ppContainer.Width = this.ppContainerEdit.Width;
                }
                this.Refresh();
                this.xucOrganization.LoadData();
                this.xucOrganization.Selected += new HRM.CORE.xucOrganization.SelectedEventHander(this.xucOrganization_Selected);
                this.ShowDisplay();
                this.xucOrganization.SelectNodeByLevelCode(this.m_Level, this.m_Code);
            }
        }

        public void LoadData(int mLevel, string mCode)
        {
            this.Level = mLevel;
            this.Code = mCode;
            if (this.xucOrganization == null)
            {
                this.xucOrganization = new HRM.CORE.xucOrganization();
                this.xucOrganization.Dock = DockStyle.Fill;
                this.ppContainer.Controls.Add(this.xucOrganization);
                if (this.ppContainer.Width < this.ppContainerEdit.Width)
                {
                    this.ppContainer.Width = this.ppContainerEdit.Width;
                }
                this.Refresh();
                this.xucOrganization.LoadData();
                this.xucOrganization.Selected += new HRM.CORE.xucOrganization.SelectedEventHander(this.xucOrganization_Selected);
                if (MyLogin.Level == this.m_Level)
                {
                    this.ShowDisplay();
                }
                this.xucOrganization.SelectNodeByLevelCode(this.m_Level, this.m_Code);
            }
        }

        private void ppContainerEdit_Popup(object sender, EventArgs e)
        {
        }

        private void RaiseDepartmentSelectedHandler(HRM.CORE.Organization Item)
        {
            if (this.DepartmentSelected != null)
            {
                this.DepartmentSelected(this, Item);
            }
        }

        private void ShowDisplay()
        {
            string str = "/";
            if (this.Level == 0)
            {
                if ("OK" == "OK")
                {
                    this.ppContainerEdit.Text = "";//MyInfo.Company;
                }
                else
                {
                    this.ppContainerEdit.Text = "Tất Cả";
                }
            }
            else
            {
                HRM_BRANCH hrm_branch;
                string str2;
                if (this.Level == 1)
                {
                    hrm_branch = new HRM_BRANCH();
                    this.BranchCode = str2 = this.Code;
                    hrm_branch.Get(str2);
                    this.ppContainerEdit.Text = hrm_branch.BranchName;
                }
                else
                {
                    HRM_DEPARTMENT hrm_department;
                    if (this.Level == 2)
                    {
                        hrm_branch = new HRM_BRANCH();
                        hrm_department = new HRM_DEPARTMENT();
                        this.DepartmentCode = str2 = this.Code;
                        hrm_department.Get(str2);
                        hrm_branch.Get(this.m_BranchCode = hrm_department.BranchCode);
                        this.ppContainerEdit.Text = hrm_branch.BranchName + str + hrm_department.DepartmentName;
                    }
                    else if (this.Level == 3)
                    {
                        hrm_branch = new HRM_BRANCH();
                        hrm_department = new HRM_DEPARTMENT();
                        HRM_GROUP hrm_group = new HRM_GROUP();
                        this.GroupCode = str2 = this.Code;
                        hrm_group.Get(str2);
                        hrm_department.Get(this.m_DepartmentCode = hrm_group.DepartmentCode);
                        hrm_branch.Get(this.m_BranchCode = hrm_department.BranchCode);
                        this.ppContainerEdit.Text = hrm_branch.BranchName + str + hrm_department.DepartmentName + str + hrm_group.GroupName;
                    }
                }
            }
            this.Organization = new HRM.CORE.Organization(this.m_BranchCode, this.m_DepartmentCode, this.m_GroupCode, this.m_EmployeeCode, this.m_Level, this.m_Code);
        }

        private void xucOrganization_Selected(object sender, HRM.CORE.Organization Item)
        {
            this.BranchCode = Item.BranchCode;
            this.DepartmentCode = Item.DepartmentCode;
            this.GroupCode = Item.GroupCode;
            this.EmployeeCode = Item.EmployeeCode;
            this.Level = Item.Level;
            this.Code = Item.Code;
            this.ShowDisplay();
            this.ppContainerEdit.ClosePopup();
            this.RaiseDepartmentSelectedHandler(Item);
        }

        private void xucOrganizationEdit_Load(object sender, EventArgs e)
        {
        }

        [Category("Column"), DisplayName("BranchCode")]
        public string BranchCode
        {
            get
            {
                return this.m_BranchCode;
            }
            set
            {
                this.m_BranchCode = value;
            }
        }

        [DisplayName("Code"), Category("Column")]
        public string Code
        {
            get
            {
                return this.m_Code;
            }
            set
            {
                this.m_Code = value;
            }
        }

        [Category("Column"), DisplayName("DepartmentCode")]
        public string DepartmentCode
        {
            get
            {
                return this.m_DepartmentCode;
            }
            set
            {
                this.m_DepartmentCode = value;
            }
        }

        [DisplayName("EmployeeCode"), Category("Column")]
        public string EmployeeCode
        {
            get
            {
                return this.m_EmployeeCode;
            }
            set
            {
                this.m_EmployeeCode = value;
            }
        }

        [Category("Column"), DisplayName("GroupCode")]
        public string GroupCode
        {
            get
            {
                return this.m_GroupCode;
            }
            set
            {
                this.m_GroupCode = value;
            }
        }

        [Category("Column"), DisplayName("Level")]
        public int Level
        {
            get
            {
                return this.m_Level;
            }
            set
            {
                this.m_Level = value;
            }
        }

        public delegate void DepartmentSelectedHandler(object sender, Organization Item);
    }
}

